package cn.dnuihyy406.dnuifood_16110100406.controller.collectfragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.CollectBean;
import cn.dnuihyy406.dnuifood_16110100406.bean.ResultBean;
import cn.dnuihyy406.dnuifood_16110100406.bean.ShopBean;
import cn.dnuihyy406.dnuifood_16110100406.controller.foodlist.FoodListActivity;
import cn.dnuihyy406.dnuifood_16110100406.controller.homefragment.ShopAdapter;
import cn.dnuihyy406.dnuifood_16110100406.listener.OnRetrofitListener;
import cn.dnuihyy406.dnuifood_16110100406.model.BaseModel;
import cn.dnuihyy406.dnuifood_16110100406.model.FoodModel;

public class CShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>implements OnRetrofitListener<ResultBean> {
    private Context mContext;
    private List<CollectBean> CollectList1;
    private LayoutInflater mLayoutInflater;
    SharedPreferences sharedPreferences;
    String user_id,shopid;
    public CShopAdapter(Context mContext, List CollectList1){
        this.mContext=mContext;
        this.CollectList1=CollectList1;
        mLayoutInflater=LayoutInflater.from(mContext);
    }
//接受上文传过来的数据，并且刷新
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v =mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.c_shop_item,viewGroup,false);
//加载适配器布局文件
        ///////
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder Holder, final int i) {
        sharedPreferences = mContext.getSharedPreferences("hyy", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getString("userid","");
        final CollectBean x=CollectList1.get(i);
        if (null==x){
            return;
        }   //创建对象 获取id数据信息
        ViewHolder viewHolder= (ViewHolder)Holder;
        viewHolder.address.setText(x.getAddress());
        viewHolder.title.setText(x.getShopname());
        Picasso.with(mContext).load(BaseModel.BaseUrl+x.getPic()).into(viewHolder.image);//接受数据，然后绑定数据显示在适配器上面。
        viewHolder.enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str = String.valueOf(CollectList1.get(i).getShop_id());
                Intent intent=new Intent(mContext, FoodListActivity.class);
                intent.putExtra("shop_id",str);
                mContext.startActivity(intent);
                Toast.makeText(mContext,x.getShopname(),Toast.LENGTH_SHORT).show();
            }
        });


        viewHolder.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ////////
                uncollect(x.getShop_id());
            }
        });
    }

    @Override
    public int getItemCount() {
        return CollectList1.size();
    }
//返回接受数据的长度。

    @Override
    public void onSuccess(ResultBean object, int flag) {   //访问服务器成功执行的代码
        if(flag==4) {
            if(object.getSuccess().equals("1")){
                Toast.makeText(mContext,"取消了",Toast.LENGTH_SHORT).show();
                for (int i=0;i<CollectList1.size();i++){
                    CollectBean y=CollectList1.get(i);
                    if(y.getShop_id().equals(shopid)){
                        CollectList1.remove(y);
                        notifyDataSetChanged();  //删除取消后的店铺
                        break;
                    }
                }
            }
        }
    }

    @Override
    public void onFailure(String msg) {
//访问服务器失败
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView title,address;
        ImageView image;
        Button enter,cancel;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);title = itemView.findViewById(R.id.food_namec);
            addres s = itemView.findViewById(R.id.shop_addressc);
            image = itemView.findViewById(R.id.food_picc);
            enter= itemView.findViewById(R.id.enter);
            cancel= itemView.findViewById(R.id.uncolloct);
        }
    }
//绑定控件~

    private void uncollect(String shop_id){
        this.shopid=shop_id;
        FoodModel foodModel = new FoodModel();
        foodModel.userCollectShop(user_id,shop_id,this);
    }
}